<template>
  <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal='false' fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="dialogFormVisible = false">关闭页面</el-button>
    <!-- 查询条件 -->
    <el-form inline label-width="70px">
      <el-form-item label="用户名:">
        <el-input v-model="searchForm.name"></el-input>
      </el-form-item>
      <el-form-item label="就诊卡号:">
        <el-input v-model="searchForm.cardNum"></el-input>
      </el-form-item>
      <el-form-item label="联系方式:">
        <el-input v-model="searchForm.telPhone"></el-input>
      </el-form-item>
      <el-form-item label="身份证号:">
        <el-input v-model="searchForm.certificateNum"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchHandle">搜索</el-button>
        <el-button icon="el-icon-delete" @click="clearSearchForm">清空</el-button>
      </el-form-item>
    </el-form>
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-button icon="el-icon-check" type="primary" @click="checkItemData" size="mini">选 择</el-button>
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 查询数据 -->
    <el-table :data="userList" border height="calc(100vh - 215px)" ref="singleTable" border style="margin-bottom: 15px;"
      header-cell-class-name="miui-header-cell-style" v-loading="dataListLoading" @selection-change="handleSelectionChange"
      @select="onTableSelect">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column key="1" prop="name" label="姓名"></el-table-column>
      <el-table-column key="2" prop="gender" label="性别">
        <template slot-scope="scope">
          <span v-if="scope.row.gender == 0">男</span>
          <span v-if="scope.row.gender == 1">女</span>
        </template>
      </el-table-column>
      <el-table-column key="3" prop="cardNum" label="就诊卡号"></el-table-column>
      <el-table-column key="4" prop="telPhone" label="联系方式"></el-table-column>
      <el-table-column key="5" prop="birthDay" label="出生年月"></el-table-column>
      <el-table-column key="6" prop="uniqueode" label="编号"></el-table-column>
    </el-table>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
  </el-dialog>
</template>

<script>
  import { fetchPersonList } from '@/api/pvm/person'
  export default {
    data() {
      return {
        dialogFormVisible: false,
        userList: [],
        searchForm: {},
        multipleSelection: '',
        formData: [],
        dataListLoading: false,
        totalPage: 0,
        pageSize: 10,
        pageIndex: 1,
        visible: false,
      }
    },
    methods: {
      init() {
        this.dialogFormVisible = true
        this.getUserList()
      },
      //  搜索查询用户
      searchHandle() {
        this.pageIndex = 1
        this.getUserList()
      },
      //  清空搜索条件
      clearSearchForm() {
        for (let key in this.searchForm) {
          this.searchForm[key] = ''
        }
      },
      //  获取用户列表
      getUserList(page, params) {
        fetchPersonList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        }, this.getUserData, this.searchForm)).then(response => {
          if (response.data.code == '0') {
            this.userList = response.data.data.records
            this.totalPage = response.data.data.total
          } else {
            this.$message.error(response.data.msg)
          }
        })
      },
      //  每页条数发生改变事件
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getUserList()
      },
      //  当前页数变化事件
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getUserList()
      },
      //  选择并关闭
      checkItemData() {
        this.dialogFormVisible = false
        this.$emit('refreshForm', this.multipleSelection)
      },
      //  判断当前操作的属性是勾选还是取消勾选
      onTableSelect(rows, row) {
        this.selected = rows.length && rows.indexOf(row) !== -1
        this.rowItem = row
      },
      handleSelectionChange(val) {
        if (this.selected) {
          val.forEach(item => {
            this.multipleSelection = item
          })
        } else {
          let that = this
          this.multipleSelection = this.multipleSelection.filter(function(item) {
            return item.id != that.rowItem.id
          })
        }
      },
    }
  }
</script>

<style scoped>

</style>
